package com.hxzy.projectssmplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.projectssmplus.dto.HouseNewDTO;
import com.hxzy.projectssmplus.entity.HouseNew;
import com.hxzy.projectssmplus.entity.SysArea;
import com.hxzy.projectssmplus.mapper.HouseNewMapper;
import com.hxzy.projectssmplus.mapper.SysAreaMapper;
import com.hxzy.projectssmplus.service.HouseNewService;
import com.hxzy.projectssmplus.vo.HouseNewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/12/08 16:34
 * Description:
 * Version: V1.0
 */
@Service
public class HouseNewServiceImpl extends ServiceImpl<HouseNewMapper, HouseNew> implements HouseNewService {

    @Autowired
    private HouseNewMapper houseNewMapper;

    @Autowired
    private SysAreaMapper sysAreaMapper;
    @Override
    public  List<HouseNewVO> searchByParamsVO(HouseNewDTO houseNewDTO) {
        List<HouseNewVO> houseNewList = this.houseNewMapper.searchByParamsVO(houseNewDTO);

        List<HouseNewVO> houseNewVOList = new ArrayList<>();
        for (HouseNewVO houseNewVO : houseNewList) {
            StringBuilder stringBuilder = new StringBuilder();
            if(houseNewVO.getPid() != null) {
                SysArea sysPArea = this.sysAreaMapper.selectById(houseNewVO.getPid());
                stringBuilder.append(sysPArea.getAreaName());
                if(houseNewVO.getCid() != null){
                    stringBuilder.append("-");
                    SysArea sysCArea = this.sysAreaMapper.selectById(houseNewVO.getCid());
                    stringBuilder.append(sysCArea.getAreaName());
                    if(houseNewVO.getAid() != null){
                        stringBuilder.append("-");
                        SysArea sysAArea = this.sysAreaMapper.selectById(houseNewVO.getAid());
                        stringBuilder.append(sysAArea.getAreaName());
                    }
                }
            }
            houseNewVO.setAreaName(stringBuilder.toString());
            houseNewVOList.add(houseNewVO);
        }
        return houseNewVOList;
    }

    @Override
    public int insertHouseNew(HouseNew houseNew){
        houseNew.setCreateTime(new Date());
        houseNew.setCreateBy("admin");
        int insert = this.houseNewMapper.insert(houseNew);
        return insert;
    }

    @Override
    public List<HouseNew> searchAll() {
        QueryWrapper<HouseNew> queryWrapper=new QueryWrapper<>();
        List<HouseNew> houseNews = this.houseNewMapper.selectList(queryWrapper);
        return houseNews;
    }



}
